package pri.damai.xiaowu.common.core.util;


import pri.damai.xiaowu.common.core.enums.ResultCommonEnum;
import pri.damai.xiaowu.common.core.enums.base.Enumerator;
import pri.damai.xiaowu.common.core.exception.GlobalException;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Stream;

/**
 * 枚举工具
 * @Desc
 * @Author DaMai
 * @Date 2021/3/23 15:29
 * 但行好事，莫问前程。
 */
public class EnumUtils {

    /**
     * 生成枚举
     * @apiNote [附加描述]
     * @param enumClasss
     * @return java.util.Map<java.lang.String, java.util.List < java.util.Map>>
     * @author DaMai
     * @date 2021/8/2 15:01
     */
    public static <E extends Enum<?> & Enumerator> Map<String, List<Map>> generateEnumsDesc(Set<Class<?>> enumClasss) {
        HashMap<String, List<Map>> map = new HashMap<>();
        for (Class clazz : enumClasss) {
            String enumClassName = clazz.getSimpleName();
            List<Map> enumList = enumToList(clazz);
            map.put(enumClassName, enumList);
        }
        return map;
    }

    /**
     * 枚举描述 --> 枚举
     * @apiNote [附加描述]
     * @param eClass 枚举 Class
     * @param desc 描述
     * @return E
     * @author DaMai
     * @date 2021/10/18 13:40
     */
    public static <E extends Enum<?> & Enumerator> E getEnumByDesc(Class<E> eClass, String desc) {
        if (StringUtils.isBlank(desc)) {
            return null;
        }

        return Stream.of(eClass.getEnumConstants())
                .filter(enumerator -> Objects.equals(enumerator.code(), desc))
                .findAny()
                .orElseThrow(() -> new GlobalException(ResultCommonEnum.ENUM_ERROR.code(),
                        String.format(ResultCommonEnum.ENUM_ERROR.desc(),
                                eClass.getSimpleName() + " desc=" + desc + " not exist")));
    }

    /**
     * 枚举值 -> 枚举类
     * @apiNote [附加描述]
     * @param enumClazz 枚举类Class
     * @param value 枚举值
     * @return E
     * @author DaMai
     * @date 2021/3/24 9:49
     */
    public static <E extends Enum<?> & Enumerator> E getEnumByValue(Class<E> enumClazz, Object value) {

        if (value == null || "".equals(value)) {
            return null;
        }

        return Stream.of(enumClazz.getEnumConstants())
                .filter(enumerator -> Objects.equals(enumerator.code(), value))
                .findAny()
                .orElseThrow(() -> new GlobalException(ResultCommonEnum.ENUM_ERROR.code(),
                        String.format(ResultCommonEnum.ENUM_ERROR.desc(),
                                enumClazz.getSimpleName() + " value=" + value + " not exist")));
    }

    /**
     * 枚举转换为List
     * @apiNote [附加描述]
     * @param clazz 枚举类Class
     * @return java.util.List<java.util.Map>
     * @author DaMai
     * @date 2021/3/24 9:49
     */
    public static <E extends Enum<?> & Enumerator> List<Map> enumToList(Class<E> clazz) {
        E[] enumConstants = clazz.getEnumConstants();
        List<Map> result = new ArrayList<>();

        for (E e : enumConstants) {
            HashMap<Object, Object> map = new HashMap<>(16);
            map.put("value", e.code());
            map.put("desc", e.desc());
            result.add(map);
        }
        return result;
    }

    public static void main(String[] args) {

        // 序列化枚举
        List<Map> maps = enumToList(ResultCommonEnum.class);
        System.out.println(maps);

        // 通过枚举值获取枚举
        ResultCommonEnum enumByValue = getEnumByValue(ResultCommonEnum.class, "000000");
        System.out.println(enumByValue);
    }
}
